package top.zsyle.product.controller;

import top.zsyle.basic.jwt.UserInfo;
import top.zsyle.basic.util.LoginContext;
import top.zsyle.pet.domain.SearchMasterMsg;
import top.zsyle.pet.dto.AcceptDto;
import top.zsyle.pet.dto.SearchMsgPublishDto;
import top.zsyle.pet.query.SearchMasterMsgQuery;
import top.zsyle.product.service.IProductMsgService;
import top.zsyle.product.domain.ProductMsg;
import top.zsyle.product.query.ProductMsgQuery;
import top.zsyle.basic.util.AjaxResult;
import top.zsyle.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/productMsg")
public class ProductMsgController {
    @Autowired
    public IProductMsgService productMsgService;


    /**
     * 保存和修改公用的
     * @param productMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ProductMsg productMsg){
        try {
            if( productMsg.getId()!=null)
                productMsgService.update(productMsg);
            else
                productMsgService.add(productMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productMsgService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public ProductMsg get(@PathVariable("id")Long id)
    {
        return productMsgService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<ProductMsg> list(){

        return productMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<ProductMsg> json(@RequestBody ProductMsgQuery query)
    {
        return productMsgService.queryByPage(query);
    }

    //立即购买
    @PostMapping("/publish/{productId}")
    public AjaxResult publish(@PathVariable("productId") Long productId)
    {
        productMsgService.publish(productId);
        return AjaxResult.me().success();
    }

    /**
     * 数据权限，待处理的分页查询
     * @param query
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<ProductMsg> tohandle(@RequestBody ProductMsgQuery query) {
        query.setState(0);
        return productMsgService.queryByPage(query);
    }

    /**
     * 接收订单
     * @param dto
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto dto) {
        productMsgService.accept(dto);
        return  AjaxResult.me().success();
    }

    /**
     * 拒单
     * @param id
     * @return
     */
    @PostMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id) {
        productMsgService.reject(id);
        return  AjaxResult.me().success();
    }

}
